<?php
class ClaycodeForm
{
	private $arr_formConfig;
	private $o_formMakerInstance;
	
	public function __construct($s_formIdent)
	{
		//retrive the form configuration first via the configuration class
		$this->arr_formConfig = Claycode_configuration::getInstance()->getFormConfig($s_formIdent , false);
		$this->makeFormMakerInstance();
		$this->setFieldSets();
		$this->setFormIdentifier();
		$this->setFormPostHandler();
		$this->o_formMakerInstance->createForm();
	}
	
	public function getInputValues()
	{
		return $this->o_formMakerInstance->getFieldValues();
	}
	
	public function setFieldValue($s_fieldId, $s_value)
	{
		$this->o_formMakerInstance->setFieldValue($s_fieldId, $s_value);
	}
	
	public function validateForm()
	{
		$this->o_formMakerInstance->validateFormInputs();
		//handles double submit after validation
		// problem with refershing the old value of the submission id of the form
		$s_formSubmitIdentifier = $this->getFormPostHandlerValue();
		$this->o_formMakerInstance->setFieldValue(Claycode_configuration::getInstance()->getFormPostSubmitHandler(), $s_formSubmitIdentifier);
		$this->writeToSessionSubmitId($s_formSubmitIdentifier);
	}
	
	public function getFormErrors()
	{
		return $this->o_formMakerInstance->getFieldErrors();
	}
	
	public function getFormHtml()
	{
		return $this->o_formMakerInstance->getFormHtml();
	}
	
	public function getFormMakerInstance()
	{
		return $this->o_formMakerInstance;
	}
	
	private function makeFormMakerInstance()
	{
		$this->o_formMakerInstance = new FormMaker(array(
			'action'=> $this->arr_formConfig['action'], 
			'method'=> $this->arr_formConfig['method'], 
			'multipart'=> $this->arr_formConfig['multipart'], 
			'form_id'=> $this->arr_formConfig['form_id'], 
			'form_line_elem'=> $this->arr_formConfig['form_line_elem'],
			'form_name'=> $this->arr_formConfig['form_name']
		));
	}
	
	private function setFieldSets()
	{
		if(!empty($this->arr_formConfig['field_sets'])){
			//loop through the fieldsets in the config 
			foreach($this->arr_formConfig['field_sets'] as $arr_fieldSetConfig){
				//set a fieldset object into the form maker instance
				$this->o_formMakerInstance->setFieldSet($arr_fieldSetConfig['field_set_id'], $arr_fieldSetConfig['field_set_legend'], $arr_fieldSetConfig['inline_errors'] );
				if(!empty($arr_fieldSetConfig['fields'])){
					foreach ($arr_fieldSetConfig['fields'] as $arr_fieldConfig){
						$this->o_formMakerInstance->setField(
							$arr_fieldConfig['field_id'], 
							$arr_fieldSetConfig['field_set_id'], 
							array(
								'label'=> $arr_fieldConfig['label'], 
								'name'=> $arr_fieldConfig['name'], 
								'value'=> $arr_fieldConfig['value'], 
								'minLength'=> $arr_fieldConfig['minLength'], 
								'maxLength'=> $arr_fieldConfig['maxLength'], 
								'type'=> $arr_fieldConfig['type'], 
								'default'=> $arr_fieldConfig['default']
							), 
							$arr_fieldConfig['rules']
						);
					}
				}
			}
		}
	}
	
	private function setFormIdentifier()
	{
		if(isset($this->arr_formConfig['field_sets'][0])){
			$arr_fieldSet = $this->arr_formConfig['field_sets'][0];
			$s_fieldSetId = $arr_fieldSet['field_set_id'];
			$s_formIdentifierFieldName = Claycode_configuration::getInstance()->getFormIdentField();
			$this->o_formMakerInstance->setField(
				$s_formIdentifierFieldName, 
				$s_fieldSetId, 
				array('label'=> '', 'name'=> $s_formIdentifierFieldName, 'value'=> $this->arr_formConfig['form_ident_alias'], 'minLength'=>5, 'maxLength'=>200, 'type'=>'hidden', 'default'=> false), 
				array(
					array('validate'=>'required', 'error_message'=> 'required value dont even try it')
				)
			);
			//$this->writeToSessionFormSubmitHandleId($this->arr_formConfig['form_ident_alias']);
		}
	}
	
	private function getFormPostHandlerValue()
	{
		$s_submitIdent = date('U');
		$s_submitIdent =  uniqid($s_submitIdent, true);
		return $s_submitIdent;
	}
	
	private function setFormPostHandler()
	{
		if(isset($this->arr_formConfig['field_sets'][0])){
			$arr_fieldSet = $this->arr_formConfig['field_sets'][0];
			$s_fieldSetId = $arr_fieldSet['field_set_id'];
			$s_formPostSubmitHandlerField = Claycode_configuration::getInstance()->getFormPostSubmitHandler();
			$s_formSubmitIdentifier = $this->getFormPostHandlerValue();
			$this->o_formMakerInstance->setField(
				$s_formPostSubmitHandlerField,
				$s_fieldSetId,
				array('label'=> '', 'name'=> $s_formPostSubmitHandlerField, 'value'=> $s_formSubmitIdentifier, 'minLength'=>5, 'maxLength'=>200, 'type'=>'hidden', 'default'=> false),
				array(
					array('validate'=>'required', 'error_message'=> 'required value dont even try it')
				)
			);
			$this->writeToSessionSubmitId($s_formSubmitIdentifier);
		}
	}
	
	public function writeToSessionSubmitId($s_submitIdent)
	{
		$_SESSION[$s_submitIdent] =  $s_submitIdent;
	}
	
}